# Audit package primer

The `audit` package is responsible for initialising and providing a configured `zap.Logger` to other packages.

## Overview

During application startup, the logger configuration is loaded from `config.GlobalConfig`. When `audit.Init()` is called, an `Auditor` instance containing the configured `zap.Logger` is returned. This `Auditor` is then propagated through middleware to various handler functions.

The audit package is initialised in `main.go` as follows:

```go
auditor, err := audit.Init(config.GlobalConfig.InDevelopment)
if err != nil {
    log.Fatalf("Failed to initialize audit logger: %v", err)
}
log.Println("Audit logger initialized")
```

### Propagation through middleware and handlers

The initialised `Auditor` is passed to the `middleware.DefineRoutes` function that sets up all application routes.

```go
router := middleware.DefineRoutes(auditor)
```

Handlers that require the configured `zap.Logger` are wrapped using `WrapWithAuditor`, providing each handler with the `Auditor` instance.

```go
router.HandleFunc("/users/{id}", CatchError(WrapWithAuditor(auditor, routes.UserPage))).Methods("GET")
```
